﻿using System;
using System.Collections.Generic;
using System.Web;
using Model;
using System.Configuration;
using System.Collections;
using System.Data;


namespace BLL
{
    public class SJA_Service
    {
        public static DataTable GetSJA_DetailData_Common(string fydm, string tsdm, string rydm, string zxblx, string begin_year, string end_year, string begin_month, string end_month, string sjatype)
        {
            DataTable result = new DataTable();
            int kind = 1;
            string tsStr = "";
            string ryStr = "";
            string tablename = "b_sja";
            switch (sjatype)
            {
                case "xs":
                    tablename = "b_xssja";
                    break;
                case "ms":
                    tablename = "b_mssja";
                    break;
                case "xz":
                    tablename = "b_xzsja";
                    break;
                case "zx":
                    tablename = "b_zxsja";
                    break;
                case "zx_new":
                    tablename = "B_ZXQKYTJB";
                    break;
            }

            if (tsdm != "0") { tsStr = " and cbspt={7} "; kind = 2; }
            if (rydm != "0") { ryStr = " and staffdm={8}"; kind = 3; }

            string selectStr = "select * from " + tablename + " where zxblx={1} and TJYear={2} and TJMonth={3} and EndYear={4} and EndMonth={5} and fydm={6}" + tsStr + ryStr;

            switch (kind)
            {
                case 1:
                    selectStr = string.Format(selectStr + " and cbspt=0 and staffdm=0", null, zxblx, begin_year, begin_month, end_year, end_month, fydm);
                    break;
                case 2:
                    selectStr = string.Format(selectStr + "and staffdm=0", null, zxblx, begin_year, begin_month, end_year, end_month, fydm, tsdm);
                    break;
                case 3:
                    selectStr = string.Format(selectStr, null, zxblx, begin_year, begin_month, end_year, end_month, fydm, tsdm, rydm);
                    break;
            }

            string conStr = DAL.DBConnector.GetConnectString(fydm);

            try
            {
                result = DAL.DBConnector.GetSqlSelectData(selectStr, conStr);
            }
            catch (Exception ex)
            {
            
            }
            return result;
        }

        public static DataTable GetSJA_DetailData_Fy(string fydm, string zxblx, string begin_year, string end_year, string begin_month, string end_month, string sjatype)
        {
            return GetSJA_DetailData_Common(fydm, "0", "0", zxblx, begin_year, end_year, begin_month, end_month, sjatype);
        }

        public static DataTable GetSJA_DetailData_Ts(string fydm, string tsdm, string zxblx, string begin_year, string end_year, string begin_month, string end_month, string sjatype)
        {
            return GetSJA_DetailData_Common(fydm, tsdm, "0", zxblx, begin_year, end_year, begin_month, end_month, sjatype);
        }

        public static DataTable GetSJA_DetailData_Ry(string fydm, string tsdm, string rydm, string zxblx, string begin_year, string end_year, string begin_month, string end_month, string sjatype)
        {
            return GetSJA_DetailData_Common(fydm, tsdm, rydm, zxblx, begin_year, end_year, begin_month, end_month, sjatype);
        }

        /// <summary>
        /// 获取参数法院及其所管辖的子法院的收结存信息
        /// </summary>
        public static DataTable GetSJA_DetailData_Fys(string ffydm, string zxblx, string begin_year, string end_year, string begin_month, string end_month, string sjatype)
        {
            DataTable dt = new DataTable();
            string tablename = "b_sja";
            switch (sjatype)
            {
                case "xs":
                    tablename = "b_xssja";
                    break;
                case "ms":
                    tablename = "b_mssja";
                    break;
                case "xz":
                    tablename = "b_xzsja";
                    break;
                case "zx":
                    tablename = "b_zxsja";
                    break;
                case "zx_new":
                    tablename = "B_ZXQKYTJB";
                    break;
            }
            DataTable dtTemp = DAL.DBConnector.GetTableSchema(ffydm, tablename);
            dtTemp = Tools.RestructDTColumn(dtTemp, "JAL");
            int columnCount = dtTemp.Columns.Count;

            Obj_B_FY[] ofys = BLL.FyDataService.GetFySelfAndChildrenFyObject(ffydm);

            DataRow row;
            foreach (Obj_B_FY fy in ofys)
            {
                dt = GetSJA_DetailData_Fy(fy.DM, zxblx, begin_year, end_year, begin_month, end_month, sjatype);
                if (dt != null)
                {
                        if (tablename == "B_ZXQKYTJB")
                        {
                            if (dt.Rows.Count == 0)
                            {
                                
                                for (int j = 0; j < 2; j++)
                                {
                                    row = dtTemp.NewRow();

                                    row = Tools.SetRowValue(row, "0");
                                    row["FYDM"] = fy.DM;

                                    row["FYMC"] = fy.MC;
                                    row["CBSPT"] = "0";

                                    if (j == 1)
                                    {
                                        row["FYMC"] = "累计";
                                    }
                                    dtTemp.Rows.Add(row);
                                }
                            }
                            else
                            {
                                for (int j = 0; j < dt.Rows.Count; j++)
                                {
                                    row = dtTemp.NewRow();

                                    for (int i = 0; i < columnCount; i++)
                                    {
                                        row[i] = dt.Rows[j][i].ToString();
                                    }

                                    if (j == 1)
                                    {
                                        row["FYMC"] = "累计";
                                    }
                                    dtTemp.Rows.Add(row);
                                }
                            }
                        }
                        else
                        {
                            if (dt.Rows.Count == 0)
                            {
                                row = dtTemp.NewRow();

                                row = Tools.SetRowValue(row, "0");
                                row["FYDM"] = fy.DM;

                                row["FYMC"] = fy.MC;
                                row["CBSPT"] = "0";

                            }
                            else
                            {
                                row = dtTemp.NewRow();

                                for (int i = 0; i < columnCount; i++)
                                {
                                    row[i] = dt.Rows[0][i].ToString();
                                }

                                dtTemp.Rows.Add(row);
                                
                            }
                        }
                        
                    
                   
                        
                }

            }
            return Format_DetailData(dtTemp);

        }

        /// <summary>
        /// 获取参数法院所有审判庭室机构的收结存信息
        /// </summary>
        public static DataTable GetSJA_DetailData_Tss(string fydm, string zxblx, string begin_year, string end_year, string begin_month, string end_month, string sjatype)
        {
            DataTable dt = new DataTable();
            string tablename = "b_sja";
            switch (sjatype)
            {
                case "xs":
                    tablename = "b_xssja";
                    break;
                case "ms":
                    tablename = "b_mssja";
                    break;
                case "xz":
                    tablename = "b_xzsja";
                    break;
                case "zx":
                    tablename = "b_zxsja";
                    break;
                case "zx_new":
                    tablename = "B_ZXQKYTJB";
                    break;
            }
            DataTable dtTemp = DAL.DBConnector.GetTableSchema(fydm, tablename);
            dtTemp = Tools.RestructDTColumn(dtTemp, "JAL");
            int columnCount = dtTemp.Columns.Count;

             Obj_B_TSJG[] otss = BLL.DepartmentService.GetDepartments(fydm);

            DataRow row;
            foreach (Obj_B_TSJG ts in otss)
            {
                dt = GetSJA_DetailData_Ts(fydm, ts.XH, zxblx, begin_year, end_year, begin_month, end_month, sjatype);
                if (dt != null)
                {
                    if (tablename == "B_ZXQKYTJB")
                    {
                        row = dtTemp.NewRow();
                        if (dt.Rows.Count == 0)
                        {
                            
                            for (int j = 0; j < 2; j++)
                            {
                                row = dtTemp.NewRow();

                                row = Tools.SetRowValue(row, "0");
                                row["fydm"] = fydm;
                                row["CBSPTMC"] = ts.MC;
                                row["CBSPT"] = ts.XH;

                                if (j == 1)
                                {
                                    row["CBSPTMC"] = "累计";
                                }
                                dtTemp.Rows.Add(row);
                            }
                        }
                        else
                        {
                            for (int j = 0; j < dt.Rows.Count; j++)
                            {
                                row = dtTemp.NewRow();

                                for (int i = 0; i < columnCount; i++)
                                {
                                    row[i] = dt.Rows[j][i].ToString();
                                }

                                if (j == 1)
                                {
                                    row["CBSPTMC"] = "累计";
                                }
                                dtTemp.Rows.Add(row);
                            }
                        }

                    }
                    else
                    {
                        row = dtTemp.NewRow();
                        if (dt.Rows.Count == 0)
                        {
                            row = Tools.SetRowValue(row, "0");
                            row["fydm"] = fydm;
                            row["CBSPTMC"] = ts.MC;
                            row["CBSPT"] = ts.XH;

                        }
                        else
                        {
                            for (int i = 0; i < columnCount; i++)
                            {
                                row[i] = dt.Rows[0][i].ToString();
                            }
                        }

                        dtTemp.Rows.Add(row);
                    }
                }

            }
            return Format_DetailData(dtTemp);

        }

        /// <summary>
        /// 获取参数法院参数机构的所有人员收结存信息
        /// </summary>
        public static DataTable GetSJA_DetailData_Rys(string fydm, string tsdm, string zxblx, string begin_year, string end_year, string begin_month, string end_month, string sjatype)
        {
            DataTable dt = new DataTable();
            string tablename = "b_sja";
            switch (sjatype)
            {
                case "xs":
                    tablename = "b_xssja";
                    break;
                case "ms":
                    tablename = "b_mssja";
                    break;
                case "xz":
                    tablename = "b_xzsja";
                    break;
                case "zx":
                    tablename = "b_zxsja";
                    break;
                case "zx_new":
                    tablename = "B_ZXQKYTJB";
                    break;
            }
            DataTable dtTemp = DAL.DBConnector.GetTableSchema(fydm, tablename);
            dtTemp = Tools.RestructDTColumn(dtTemp, "JAL");
            int columnCount = dtTemp.Columns.Count;

            Obj_B_RYXX[] orys = BLL.PersonService.GetPersons(fydm,tsdm);

            DataRow row;
            foreach (Obj_B_RYXX ry in orys)
            {
                dt = GetSJA_DetailData_Ry(fydm, tsdm, ry.DM, zxblx, begin_year, end_year, begin_month, end_month, sjatype);
                if (dt != null)
                {
                    if (tablename == "B_ZXQKYTJB")
                    {
                        row = dtTemp.NewRow();
                        if (dt.Rows.Count == 0)
                        {

                            for (int j = 0; j < 2; j++)
                            {
                                row = dtTemp.NewRow();

                                row = Tools.SetRowValue(row, "0");
                                row["fydm"] = fydm;
                                row["CBSPT"] = tsdm;
                                row["STAFF"] = ry.MC;
                                row["STAFFDM"] = ry.DM;

                                if (j == 1)
                                {
                                    row["STAFF"] = "累计";
                                }
                                dtTemp.Rows.Add(row);
                            }
                        }
                        else
                        {
                            for (int j = 0; j < dt.Rows.Count; j++)
                            {
                                row = dtTemp.NewRow();

                                for (int i = 0; i < columnCount; i++)
                                {
                                    row[i] = dt.Rows[j][i].ToString();
                                }

                                if (j == 1)
                                {
                                    row["STAFF"] = "累计";
                                }
                                dtTemp.Rows.Add(row);
                            }
                        }
                    }
                    else
                    {
                        row = dtTemp.NewRow();
                        if (dt.Rows.Count == 0)
                        {
                            row = Tools.SetRowValue(row, "0");
                            row["fydm"] = fydm;
                            row["CBSPT"] = tsdm;
                            row["STAFF"] = ry.MC;
                            row["STAFFDM"] = ry.DM;

                        }
                        else
                        {
                            for (int i = 0; i < columnCount; i++)
                            {
                                row[i] = dt.Rows[0][i].ToString();
                            }
                        }
                        dtTemp.Rows.Add(row);
                    }
                    
                }

            }
            return Format_DetailData(dtTemp);

        }

        /// <summary>
        /// 格式化符合要求的DataTable,添加mc一列。要求：该DataTables要有FYMC、CBSPTMC、CBSPT、STAFFDM这4列，当CBSPT的值为0时，mc列的值为FYMC的值，否则为CBSPTMC的值。
        /// </summary>
        private static DataTable Format_DetailData(DataTable dt)
        {

            if (!dt.Columns.Contains("mc")) dt.Columns.Add(new DataColumn("mc", typeof(string)));

            foreach (DataRow row in dt.Rows)
            {
                if (row["STAFFDM"].ToString() != "0")
                {
                    row["mc"] = row["STAFF"].ToString();
                }
                else if (row["CBSPT"].ToString() != "0")
                {
                    row["mc"] = row["CBSPTMC"].ToString();
                }
                else { row["mc"] = row["FYMC"].ToString(); }
            }
            return dt;
        }
        //---------------------------------------------------------------
        // 实时查询
        //---------------------------------------------------------------
        public static DataTable GetSJA_Pro(string ffydm, string fydm, string tsdm, string ksrq, string jsrq, string sjatype)
        {
            DataTable result = new DataTable();
            result = DAL.DBConnector.GetPro_JAL(ffydm, fydm, tsdm, ksrq, jsrq, sjatype);
            return result;
            
        }

        public static DataTable GetSJA_FC_Pro(string fymc, string tsmc, string rymc, string ksrq, string jsrq, string lxh, string fydm, string sjatype)
        {
            DataTable result = new DataTable();
            result = DAL.DBConnector.GetPro_JAL_FC(fymc, tsmc, rymc, ksrq, jsrq, lxh, fydm, sjatype);
            return result;

        }

        //---------------------------------------------------------------
        // 实时查询
        //---------------------------------------------------------------
        public static DataTable GetCKQK_Pro(string ffydm, string fydm, string ksrq, string jsrq)
        {
            DataTable result = new DataTable();
            result = DAL.DBConnector.GetCKQK_Pro(ffydm, fydm, ksrq, jsrq);
            return result;

        }

        public static DataTable GetCKQK_FC_Pro(string fymc, string rymc, string ksrq, string jsrq, string lxh)
        {
            DataTable result = new DataTable();
            result = DAL.DBConnector.GetCKQK_FC_Pro(fymc, rymc, ksrq, jsrq, lxh);
            return result;

        }

        //执行跟踪统计反查

        public static DataTable GetZXGZTJ_FC_Pro(string WebPageName,string fydm,string tsmc, string rymc, string ksrq, string jsrq, string lxh)
        {
            DataTable result = new DataTable();
            result = DAL.DBConnector.GetZXGZTJ_FC_Pro(WebPageName, fydm, tsmc, rymc, ksrq, jsrq, lxh);
            return result;

        }
    }
}